package com.tmzh.controller;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author zpeng
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {
	private static final Integer SUCCESS = 0;
	private static final Integer FAIL = 1;
	private static final long serialVersionUID = 1L;

	@ApiModelProperty("0-成功，1-失败，2-批量操作结果")
	@Getter
	@Setter
	private int code;

	@Getter
	@Setter
	private String msg;

	@Getter
	@Setter
	private T data;

	public static <T> Result<T> ok() {
		return restResult(null, SUCCESS, "success");
	}

	public static <T> Result<T> ok(T data) {
		return restResult(data, SUCCESS, "success");
	}


	public static <T> Result<T> ok(T data, String msg) {
		return restResult(data, SUCCESS, msg);
	}



	public static <T> Result<T> failed() {
		return restResult(null, FAIL, "fail");
	}

	public static <T> Result<T> failed(String msg) {
		return restResult(null, FAIL, msg);
	}

	public static <T> Result<T> failed(int code,String msg) {
		return restResult(null, code, msg);
	}


	public static <T> Result<T> failed(T data, String msg) {
		return restResult(data, FAIL, msg);
	}

	public static <T> Result<T> restResult(T data, int code, String msg) {
		Result<T> apiResult = new Result<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

}
